#ifndef __OBJECTMESH__
#define __OBJECTMESH__

#include "BVH.h"
#include "GeometricObjects.h"
#include <string>
#include "ObjLoader.h"
#include <iostream>


using std::string;
using std::cout;
using std::endl;

namespace Engine
{
	class ObjectMesh : public GeometricObjects
	{
	public:
		vector<GeometricObjects*> m_objects;

		ObjectMesh()
		{}

		ObjectMesh(string p_path, Point3D centorid = Point3D(0), bool p_useTexture = false)
		{
			clock_t startingTimer = clock(), endingTimer;
			m_objects = ObjLoader().LoadFile(p_path, centorid, p_useTexture);
			endingTimer = clock();
			double timeInSeconds = ((double)endingTimer - startingTimer) / (double)CLOCKS_PER_SEC;
			Logger().LogTrace(to_string(timeInSeconds));

			/*for (int index = 0; index < m_objects.size(); index ++)
			{
				if (m_objects[index]->GetMaterial()->GetMaterialName() != "kamen_zid")
				{
					cout << "error here" << endl;
				}
			}*/

		}

		using GeometricObjects::MakeBoundingBox;
		void MakeBoundingBox()
		{
			box = accelerationStructure->rootNode->boundingBox;
		}

		virtual bool hit(Ray& p_ray, ShadeRec& sr)
		{
			double tmin = INT_MAX;
			return false;
		}

		using GeometricObjects::hit;
		virtual bool hit(Point3D o, Point3D  d, double& tmin, ShadeRec& sr)
		{
			double tmax = INT_MIN;
			Ray currentRay(o, d);
			bool hitObject = accelerationStructure->TestIntersection(currentRay, sr, tmin);
			return hitObject;
		}

		using GeometricObjects::GetNormal;
		virtual Point3D GetNormal(Point3D p_location)
		{
			//TODO fix this 
			return Point3D();
		}

		void BuildScene()
		{
			accelerationStructure = new BVH(m_objects, MIDDLE_POINT, true);
			MakeBoundingBox();
		}

		~ObjectMesh()
		{}

		virtual void PrintObjectName()
		{
			Logger::LogInfo("Triangle soup " + GetMaterial()->GetMaterialName() + "---------");
		}



	private:
		BVH* accelerationStructure;
		
	protected:
	};
}
#endif // !__OBJECTMESH__
